import os
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QDesktopWidget, QDialog, QWidget
from PyQt5 import QtGui, QtWidgets
# from PyQt5.QtCore import QFile
# from PySide2 import QtGui, QtWidgets
# from PySide2.QtWidgets import QApplication, QWidget, QMainWindow, QDesktopWidget, QDialog
from PySide2.QtCore import QFile
from PySide2.QtUiTools import QUiLoader
from P3_main import Ui_P3_main
from about import Ui_Dialog
import apprcc_rc


def center(self):
    screen = QDesktopWidget().screenGeometry()
    size = self.geometry()
    self.move(int((screen.width() - size.width()) / 2), int((screen.height() - size.height()) / 2))


class AboutDialog(QMainWindow, Ui_Dialog):
    def __init__(self):
        super(AboutDialog, self).__init__()
        self.setupUi(self)
        center(self)


# class MyMainWidgets(QMainWindow, Ui_P3_main):
class MyMainWidgets(QWidget):
    def __init__(self):
        super(MyMainWidgets, self).__init__()
        self.winabout = AboutDialog()
        self.load_ui()
        # self.setupUi(self)
        center(self)
        self.about_Btn.clicked.connect(self.aboutdialogshow)
        self.openfile_Btn.clicked.connect(self.open_file)

    def load_ui(self):
        loader = QUiLoader()
        path = os.path.join(os.path.dirname(__file__), "P3_main.ui")
        ui_file = QFile(path)
        ui_file.open(QFile.ReadOnly)
        loader.load(ui_file, self)
        ui_file.close()

    def open_file(self):
        filename, filetype = QtWidgets.QFileDialog.getOpenFileName(self, "选取文件", os.getcwd(),
                                                                   "All Files(*);;Text Files(*.txt);;Sql Files(*.sqlite)")
        return filename

    def aboutdialogshow(self):
        self.winabout.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    myWin = MyMainWidgets()
    myWin.show()
    sys.exit(app.exec_())
